<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Product
 *
 * @property int $id
 * @property string $name
 * @property float $price
 * @property int $stock
 * @property int $category_id
 * @property string $main_img_url
 * @property int $from
 * @property string $summary
 * @property int $img_id
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereFrom($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereMainImgUrl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereStock($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereSummary($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Product extends Model
{
    /**
     * @var array
     */
    protected $hidden = ['created_at', 'updated_at', 'category_id', 'pivot', 'from'];

    /**
     * 拼接URL路径
     * @param $value
     * @return string
     */
    public function getMainImgUrlAttribute($value)
    {
        if ($this->from == 1){
            return env('IMG_PREFIX') . $value;
        }else{
            return $value;
        }
    }

    /**
     * 申明和images的一对多的关系
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function images()
    {
        return $this->hasMany('App\Models\ProductImage', 'product_id', 'id');
    }

    /**
     * 申明和productProperty一对多的关系
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function properties()
    {
        return $this->hasMany('App\Models\ProductProperty', 'product_id', 'id');
    }
}
